package com.ms.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 */
public class NumberUtil {


    public static Number parseNumber(String pattern, String number) throws Exception {
        DecimalFormat decimalFormat = new DecimalFormat(pattern);
        return decimalFormat.parse(number);
    }


    public static Long parseLong(String invoiceDraft) {
        try{
            return Long.parseLong(invoiceDraft);
        }catch (NumberFormatException ignored){
            return null;
        }
    }

    public static Integer parseInteger(String invoiceDraft) {
        try{
            return Integer.parseInt(invoiceDraft);
        }catch (NumberFormatException ignored){
            return null;
        }
    }

    public static int nvl(Integer number, int valueIdNull) {
        return number==null?valueIdNull:number;
    }
    public static long nvl(Long number, long valueIdNull) {
        return number==null?valueIdNull:number;
    }

    public static <T extends Number> T nvl(T n1, T n2) {
        return n1!=null?n1:n2;
    }

    public static BigDecimal fix2Dec(BigDecimal monto) {
        return monto==null?null: monto.setScale(2, BigDecimal.ROUND_HALF_UP);
    }
}
